//
//  DEMOMenuViewController.m
//  RESideMenuExample
//
//  Created by Roman Efimov on 10/10/13.
//  Copyright (c) 2013 Roman Efimov. All rights reserved.
//

#import "DEMOLeftMenuViewController.h"

#import "FLMHomeVC.h"
#import "FLMOrderManagement.h"
#import "FLMManagerOrderController.h"
#import "FLMNavigationController.h"
#import "AppDelegate.h"

#import "FLMLeftMenuCell.h"
#import "FLMImagePickerVC.h"
#import "FLMLoginVC.h"

#import "FLMSearchVC.h"
#import "HomeViewController.h"

#import "FLMBackProfitController.h"
#import "FLMPinkageViewController.h"
#import "FLMTenNineSearchVC.h"
@interface DEMOLeftMenuViewController ()<FLMImagePickerVCDelegate,UIActionSheetDelegate>
{
    UIImageView *iconView;
    UIButton *button;
    NSArray *_titleArr;
    NSArray *_imagesArr;
    XLsn0wPageController *pageVC;
    
}


@property (strong, readwrite, nonatomic) UITableView *tableView;

@end

@implementation DEMOLeftMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _titleArr = @[@"首页", @"高拥返利", @"19.9包邮", @"9.9包邮", @"订单管理",@"兑换",@"退出"];
    _imagesArr = @[@"IconHome", @"IconCalendar", @"IconProfile", @"IconSettings", @"IconHome",@"IconHome",@"IconHome"];

    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 50 * 10) / 2.0f, self.view.frame.size.width, 50 * 10) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.opaque = NO;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView = nil;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView registerClass:[FLMLeftMenuCell class] forCellReuseIdentifier:@"FLMLeftMenuCell"];
        tableView.bounces = NO;
        tableView;
        
        
    });
    [self.view addSubview:self.tableView];

}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    
    UIView *bottomView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, self.view.frame.size.width, 100))];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(iconView.centerX - 40 ,50,80,80);
    button.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [button setTitle:@"登录/注册" forState:UIControlStateNormal];
    [button setTitleColor:white forState:UIControlStateNormal];
    [button setTitle:@"登录/注册" forState:UIControlStateSelected];
    [button setTitleColor:orange forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:button];
    self.tableView.tableHeaderView = bottomView;
    
    
    iconView = [[UIImageView alloc] init];
    iconView.backgroundColor = [UIColor orangeColor];
    UIImage *sandBoxImage=[FLMImagePickerVC loadImageFromSandbox:@"image"];
    iconView.image = sandBoxImage;
    iconView.frame = CGRectMake(100 ,0,70,70);
    iconView.layer.cornerRadius = 35;
    iconView.userInteractionEnabled = YES;
    iconView.layer.masksToBounds = YES;
    [bottomView addSubview:iconView];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addUserImagePicker)];
    [iconView addGestureRecognizer:singleTap];
    self.tableView.tableHeaderView = bottomView;
}


#pragma mark --------------------
#pragma mark 登录或注册
- (void)btnClick:(UIButton *)btn
{
    FLMLoginVC *loginVc = [[FLMLoginVC alloc] init];
    [self presentViewController:[[UINavigationController alloc]initWithRootViewController:loginVc] animated:YES completion:nil];
    [self.sideMenuViewController hideMenuViewController];
    NSLog(@"进去登录");
}

#pragma mark --------------------
#pragma mark添加用户头像
- (void)addUserImagePicker
{
    //判断是否支持相机  模拟器去除相机功能
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打开照相机",@"从相册上传" ,nil];
        [sheet showInView:self.view];
    }else{
        UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册上传" ,nil];
        [sheet showInView:self.view];
    }
}

#pragma mark-----UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        switch (buttonIndex) {
            case 0:{
                [self presentViewController:ImagePickerStyleCamera];
            }
                break;
            case 1:{
                [self presentViewController:ImagePickerStylePhotoLibrary];
            }
                break;
            default:
                break;
        }
    }else{
        switch (buttonIndex) {
            case 0:{
                [self presentViewController:ImagePickerStylePhotoLibrary];
            }
                break;
            default:
                break;
        }
    }
}
#pragma mark----跳转到FLMImagePickerVC
-(void)presentViewController:(imagePickerStyle)style
{
    FLMImagePickerVC *picker=[[FLMImagePickerVC alloc]initWithImagePickerStyle:style];
    picker.FLMDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}
#pragma mark----FLMImagePickerVCDelegate
-(void)imageChooseFinish:(UIImage *)image
{
    [iconView setImage:image];
    //保存到沙盒中
    [FLMImagePickerVC saveImageToSandbox:image andImageNage:@"image" andResultBlock:^(BOOL success) {
        NSLog(@"保存成功");
    }];
}


#pragma mark -
#pragma mark UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    RESideMenu *menu = delegate.sideMenuViewController;
    UITabBarController *tabVC = (UITabBarController *)menu.contentViewController;
    
    
    if (indexPath.row == 0) {
        //首页
        [self.sideMenuViewController hideMenuViewController];
    } else if (indexPath.row == 1) {
        //高拥返利
        FLMBackProfitController * backP = [[FLMBackProfitController alloc]init];
        [tabVC.selectedViewController pushViewController:backP animated:YES];
        [self.sideMenuViewController hideMenuViewController];
        
    } if (indexPath.row == 2) {
        //19.9包邮
        XLsn0wPageController *pageController = [self tenNineCreateXLsn0wPageControllers];
        pageController.title = @"19.9包邮";
        pageController.menuViewStyle = WMMenuViewStyleLine;
        [tabVC.selectedViewController pushViewController:pageController animated:YES];
        [self.sideMenuViewController hideMenuViewController];
        
    } if (indexPath.row == 3) {
        //9.9包邮
        XLsn0wPageController *pageController = [self createXLsn0wPageControllers];
        pageController.title = @"9.9包邮";
        pageController.menuViewStyle = WMMenuViewStyleLine;
        [tabVC.selectedViewController pushViewController:pageController animated:YES];
        [self.sideMenuViewController hideMenuViewController];
    } if (indexPath.row == 4) {
        //订单管理
        FLMManagerOrderController *orderNav = [[FLMManagerOrderController alloc] init];
        [tabVC.selectedViewController pushViewController:orderNav animated:YES];
        [self.sideMenuViewController hideMenuViewController];
    } if (indexPath.row == 5) {
        
        
    } else {
        
    }
}

#pragma mark --------------------
#pragma mark 9.9包邮
- (XLsn0wPageController *)createXLsn0wPageControllers {
    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
    NSMutableArray *titles = [[NSMutableArray alloc] init];
    NSArray *titleArray = @[@"全部", @"数码", @"汽车",@"美女",@"鸡腿", @"苹果", @"社会",@"图片",@"广州", @"婚恋"];
    for (int i = 0; i < [titleArray count]; i++) {
        Class vcClass;
        NSString *title;
        
        /**
         *  判断 添加子控制器
         */
        if (i == 0 || i == 2 || i== 4) {
            vcClass = [HomeViewController class];
        } else {
            vcClass = [HomeViewController class];
        }
        
        title = [titleArray objectAtIndex:i];
        [viewControllers addObject:vcClass];
        [titles addObject:title];
    }
    pageVC = [[XLsn0wPageController alloc] initWithViewControllerClasses:viewControllers andTheirTitles:titles];
    pageVC.menuItemWidth = 85;
    pageVC.postNotification = YES;
    pageVC.bounces = YES;
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setImage:[UIImage imageNamed:@"Search"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(searchMore) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    pageVC.navigationItem.rightBarButtonItem = item;
    
    return pageVC;
}

#pragma mark --------------------
#pragma mark 9.9搜索
- (void)searchMore
{
    XLsn0wLog(@"搜索");
    
    FLMSearchVC *searchVc = [[FLMSearchVC alloc] init];
        
    [pageVC.navigationController pushViewController:searchVc animated:YES];
}




#pragma mark --------------------
#pragma mark 19.9包邮
- (XLsn0wPageController *)tenNineCreateXLsn0wPageControllers {
    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
    NSMutableArray *titles = [[NSMutableArray alloc] init];
    NSArray *titleArray = @[@"全部", @"新闻", @"科技",@"娱乐",@"头条", @"艺术", @"视频",@"逗图",@"内涵", @"社区"];
    for (int i = 0; i < [titleArray count]; i++) {
        Class vcClass;
        NSString *title;
        
        /**
         *  判断 添加子控制器
         */
        if (i == 0 || i == 2 || i== 4) {
            vcClass = [FLMPinkageViewController class];
        } else {
            vcClass = [FLMPinkageViewController class];
        }
        
        title = [titleArray objectAtIndex:i];
        [viewControllers addObject:vcClass];
        [titles addObject:title];
    }
    pageVC = [[XLsn0wPageController alloc] initWithViewControllerClasses:viewControllers andTheirTitles:titles];
    pageVC.menuItemWidth = 85;
    pageVC.postNotification = YES;
    pageVC.bounces = YES;
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setImage:[UIImage imageNamed:@"Search"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(searchMore) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    pageVC.navigationItem.rightBarButtonItem = item;
    
    return pageVC;
}

#pragma mark --------------------
#pragma mark 19.9包邮搜索
- (void)searchMoreShop
{
    XLsn0wLog(@"搜索");
    
    FLMTenNineSearchVC *searchVc = [[FLMTenNineSearchVC alloc] init];
    
    [pageVC.navigationController pushViewController:searchVc animated:YES];
}

#pragma mark -
#pragma mark UITableView Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FLMLeftMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLMLeftMenuCell"];
    [cell configCellWithTitle:_titleArr[indexPath.row] icon:_imagesArr[indexPath.row]];
    
//    NSLog(@"cell= %@",NSStringFromCGSize(cell.frame.size));
    
    return cell;
}

@end
